<?php session_start();
require_once('Connections/db.php'); 
require_once('funciones/funciones.php');

$MM_authorizedUsers = GetGruposAutorizados($database_db, $db, 8);//zonas;
$MM_donotCheckaccess = "false";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && false) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "loginfailed.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
  //to fully log out a visitor we need to clear the session varialbles
  session_unregister('MM_Username');
  session_unregister('MM_UserGroup');
	
  $logoutGoTo = "index.php";
  if ($logoutGoTo) {
    header("Location: $logoutGoTo");
    exit;
  }
}
$currentPage = $_SERVER["PHP_SELF"];

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO zonas (descripcion) VALUES (%s)",
                       GetSQLValueString($_POST['descripcion'], "text"));

  mysql_select_db($database_db, $db);
  $Result1 = mysql_query($insertSQL, $db) or die(mysql_error());

  $insertGoTo = "zonas.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

$maxRows_qryzonas = 10;
$pageNum_qryzonas = 0;
if (isset($_GET['pageNum_qryzonas'])) {
  $pageNum_qryzonas = $_GET['pageNum_qryzonas'];
}
$startRow_qryzonas = $pageNum_qryzonas * $maxRows_qryzonas;

mysql_select_db($database_db, $db);
$query_qryzonas = "select * from zonas order by descripcion asc";
$query_limit_qryzonas = sprintf("%s LIMIT %d, %d", $query_qryzonas, $startRow_qryzonas, $maxRows_qryzonas);
$qryzonas = mysql_query($query_limit_qryzonas, $db) or die(mysql_error());
$row_qryzonas = mysql_fetch_assoc($qryzonas);

if (isset($_GET['totalRows_qryzonas'])) {
  $totalRows_qryzonas = $_GET['totalRows_qryzonas'];
} else {
  $all_qryzonas = mysql_query($query_qryzonas);
  $totalRows_qryzonas = mysql_num_rows($all_qryzonas);
}
$totalPages_qryzonas = ceil($totalRows_qryzonas/$maxRows_qryzonas)-1;

$queryString_qryzonas = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_qryzonas") == false && 
        stristr($param, "totalRows_qryzonas") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_qryzonas = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_qryzonas = sprintf("&totalRows_qryzonas=%d%s", $totalRows_qryzonas, $queryString_qryzonas);

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><!-- InstanceBegin template="/Templates/menu.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- InstanceBeginEditable name="doctitle" -->
<title>Aduana</title>
<!-- InstanceEndEditable --><!-- InstanceBeginEditable name="head" -->
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' es requerido.\n'; }
  } if (errors) alert('Error:\n'+errors);
  document.MM_returnValue = (errors == '');
}
function BRB_PHP_DelWithCon(deletepage_url, messagetext) { //v1.0 - Deletes a record with confirmation
  if (confirm(messagetext)==1){
  	location.href = eval('\"'+deletepage_url+'\"');
  }
}
function gotofocus() {
	document.form1.descripcion.focus();
}
//-->
</script>
<!-- InstanceEndEditable -->
<link href="aduana.css" rel="stylesheet" type="text/css">
<script language="JavaScript" type="text/JavaScript">
<!-- 

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
 

//-->
</script>
<style type="text/css">
<!--
body {

}
.style1 {color: #FFFFFF}
-->
</style>
<!-- InstanceParam name="OptionalRegion1" type="boolean" value="true" --><!-- InstanceParam name="OptionalRegion2" type="boolean" value="true" --><!-- InstanceParam name="OptionalRegion3" type="boolean" value="true" -->
</head>

<body onLoad="gotofocus()">
<table width="720" height="100%" border="2" align="center" cellpadding="0" cellspacing="0" bordercolor="#333399">
  <tr>
    <td valign="top"><table width="780" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="600" valign="middle">&nbsp;</td>
        <td><table width="180" height="60" border="0" cellpadding="0" cellspacing="1" bgcolor="#757B93">
            <tr>
              <td bgcolor="#bdbdbd"><div align="center" class="logout_usr">
			    <?php if (isset($_SESSION['MM_Username'])) {
				?>
			    <a href="<?php echo $logoutAction ?>" class="logout_usr">
					<?php  echo 'logout: '.$_SESSION['MM_Username']; ?>
				  </a>			  	
			    <?php } else {?>
				  <a href="index.php" class="negritaBlanca">Login</a></div></td> 
			  <?php } ?>
            </tr>
            <tr>
              <td height="20" class="titulo_adm_actual"><!-- InstanceBeginEditable name="titulo_adm_actual" -->
                <div align="center">Zonas de Operaci&oacute;n </div>
              <!-- InstanceEndEditable --></td>
            </tr>
        </table></td>
      </tr>
    </table>
      
      <table width="780" height="20" border="0" cellpadding="0" cellspacing="1" bgcolor="#1B01A9">
        <tr bgcolor="#000099">
		<?php if (isset($_SESSION['MM_Username']) ) { 
			echo '<td bgcolor="#757B93">&nbsp;<a href="servicio_add.php" class="menu_principal">Servicios</a>&nbsp;| &nbsp;<a href="liquidaciones.php" class="menu_principal">Liquidaciones</a>&nbsp;| &nbsp;<a href="pagos.php" class="menu_principal">Pagos</a>&nbsp;| &nbsp;<a href="basicos.php" class="menu_principal">Tablas</a>&nbsp;| &nbsp;<a href="agentes.php" class="menu_principal">Agentes</a>&nbsp;| &nbsp; <a href="informes.php" class="menu_principal">Informes</a>&nbsp;| &nbsp; <a href="permisos.php" class="menu_principal">Seguridad</a> </td>';
		} else {  
			echo '<td bgcolor="#757B93" class="menu_principal">&nbsp;Servicios &nbsp;| &nbsp;Liquidaciones&nbsp;| &nbsp;Pagos&nbsp;| &nbsp;Despachantes&nbsp;| &nbsp;Operadores &nbsp;| &nbsp;Lugares &nbsp;| &nbsp;Agentes&nbsp;| &nbsp;Seguridad</td>';						
		}?>
		<td width="178" class="negritaBlanca style1"><div align="center">
              <SCRIPT language=JavaScript><!--
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
var now = new Date();
var textout; 
var month = now.getMonth();
var date = now.getDate();
var year = now.getYear();
if (month==0) textout="Enero";
if (month==1) textout="Febrero";
if (month==2) textout="Marzo";
if (month==3) textout="Abril";
if (month==4) textout="Mayo";
if (month==5) textout="Junio";
if (month==6) textout="Julio";
if (month==7) textout="Agosto";
if (month==8) textout="Septiembre";
if (month==9) textout="Octubre";
if (month==10) textout="Noviembre";
if (month==11) textout="Diciembre";
//textout += " " + date + ", " + y2k(year);
document.write (date + " de " + textout + " de " + y2k(year) );
// -->
</SCRIPT>
          </div></td>
        </tr>
      </table>
      <table width="780" height="400" border="0" cellpadding="0" cellspacing="1">
        <tr>
          <td width="140" valign="top" bgcolor="#999999"><table width="140" border="0" cellpadding="0" cellspacing="1" bgcolor="#bdbdbd">
             
              <!-- InstanceBeginEditable name="EditRegion4" -->
              <tr>
                <td height="20" bgcolor="#757B93" class="titulo_adm_actual">&nbsp;</td>
              </tr>
              <tr>
                <td height="20" bgcolor="#999999"><table width="140" border="0" cellpadding="0" cellspacing="1" bgcolor="#bdbdbd">
                  <tr>
                    <td height="20" bgcolor="#999999" class="menu_izquierda"><div align="right"><a href="despachantes.php" class="menu_izquierda">Despachantes</a></div></td>
                  </tr>
                  <tr>
                    <td height="20" bgcolor="#999999" class="menu_izquierda"><a href="operadores.php" class="menu_izquierda">Operadores</a></td>
                  </tr>
                  <tr bgcolor="#bdbdbd">
                    <td height="20" bgcolor="#999999" class="menu_izquierda"><div align="right"><a href="lugares.php" class="menu_izquierda">Lugares</a> </div></td>
                  </tr>
                  <tr bgcolor="#bdbdbd">
                    <td height="20" bgcolor="#999999"><div align="right"><a href="regimenes.php" class="menu_izquierda">Regimen</a></div></td>
                  </tr>
                  <tr bgcolor="#bdbdbd">
                    <td height="20" bgcolor="#999999"><div align="right"><a href="diasinhabiles.php" class="menu_izquierda">D&iacute;as inh&aacute;biles </a></div></td>
                  </tr>
                  <tr bgcolor="#bdbdbd">
                    <td height="20" bgcolor="#999999"><div align="right"><a href="funcionesagentes.php" class="menu_izquierda">Funciones agentes </a></div></td>
                  </tr>
                  <tr bgcolor="#bdbdbd">
                    <td height="20" bgcolor="#999999"><div align="right"><span class="menu_izquierda"><a href="comidas.php" class="menu_izquierda">Comidas</a></span></div></td>
                  </tr>
                </table> </td>
              </tr>
              <!-- InstanceEndEditable -->
          </table>
          <br></td>
          <td valign="top" bgcolor="#FFFFFF"><!-- InstanceBeginEditable name="EditRegion1" -->EditRegi
              <?php if ($totalRows_qryzonas > 0) { // Show if recordset not empty ?>
              <table width="60%" border="0" align="center">
                <tr>
                  <td colspan="2" class="titulo_columnas_tabla">&nbsp;</td>
                  <td colspan="2" class="titulo_columnas_tabla"><div align="center">Descripcion</div></td>
                </tr>
                <?php do { ?>
                <tr>
                  <td width="17" class="contenido_columnas_tabla"><?php if( GetPermiso($database_db, $db, 8, 1) ) { ?>
                    <a href="javascript:BRB_PHP_DelWithCon('delzona.php?cod_zona=<?php echo $row_qryzonas['cod_zona']; ?>','&iquest;Eliminar la Zona?');"><img src="imagenes/delete.jpg" alt="Eliminar la Zona" title="Eliminar la Zona" width="16" height="16" border="0"></a>
                    <?php  } else { ?>
                    <img src="imagenes/delete.jpg" alt="No est&aacute; autorizado a eliminar Zonas" title="No est&aacute; autorizado a eliminar Zonas" width="16" height="16" border="0">
                    <?php } ?></td>
                  <td width="17" class="contenido_columnas_tabla"><?php if( GetPermiso($database_db, $db, 8, 2) ) { ?>
                    <a href="modzona.php?cod_zona=<?php echo $row_qryzonas['cod_zona'];?>"><img src="imagenes/edit_16.jpg" alt="Modificar datos de la Zona" title="Modificar datos de la Zona" width="16" height="16" border="0"></a>
                    <?php  } else { ?>
                    <img src="imagenes/edit_16.jpg" alt="No est&aacute; autorizado a modificar datos de Zonas" title="No est&aacute; autorizado a modificar datos de Zonas" width="16" height="16" border="0">
                    <?php } ?></td>
                  <td width="29" class="contenido_columnas_tabla">&nbsp;</td>
                  <td width="301" class="contenido_columnas_tabla"><?php echo $row_qryzonas['descripcion']; ?></td>
                </tr>
                <?php } while ($row_qryzonas = mysql_fetch_assoc($qryzonas)); ?>
              </table>
              <table border="0" width="50%" align="center">
                <tr>
                  <td width="23%" align="center">
                    <?php if ($pageNum_qryzonas > 0) { // Show if not first page ?>
                    <a href="<?php printf("%s?pageNum_qryzonas=%d%s", $currentPage, 0, $queryString_qryzonas); ?>"><img src="First.gif" border=0></a>
                    <?php } // Show if not first page ?>
                  </td>
                  <td width="31%" align="center">
                    <?php if ($pageNum_qryzonas > 0) { // Show if not first page ?>
                    <a href="<?php printf("%s?pageNum_qryzonas=%d%s", $currentPage, max(0, $pageNum_qryzonas - 1), $queryString_qryzonas); ?>"><img src="Previous.gif" border=0></a>
                    <?php } // Show if not first page ?>
                  </td>
                  <td width="23%" align="center">
                    <?php if ($pageNum_qryzonas < $totalPages_qryzonas) { // Show if not last page ?>
                    <a href="<?php printf("%s?pageNum_qryzonas=%d%s", $currentPage, min($totalPages_qryzonas, $pageNum_qryzonas + 1), $queryString_qryzonas); ?>"><img src="Next.gif" border=0></a>
                    <?php } // Show if not last page ?>
                  </td>
                  <td width="23%" align="center">
                    <?php if ($pageNum_qryzonas < $totalPages_qryzonas) { // Show if not last page ?>
                    <a href="<?php printf("%s?pageNum_qryzonas=%d%s", $currentPage, $totalPages_qryzonas, $queryString_qryzonas); ?>"><img src="Last.gif" border=0></a>
                    <?php } // Show if not last page ?>
                  </td>
                </tr>
              </table>
              <?php } // Show if recordset not empty ?>
              <br>
              <form action="<?php echo $editFormAction; ?>" method="post" name="form1" onSubmit="MM_validateForm('descripcion','','R');return document.MM_returnValue">
                <table align="center">
                  <tr valign="baseline">
                    <td colspan="2" align="right" nowrap class="titulo_form_ingreso_datos"><div align="center">Nueva zona </div></td>
                  </tr>
                  <tr valign="baseline">
                    <td align="right" nowrap class="labels_ingreso_datos">Descripcion:</td>
                    <td><input name="descripcion" type="text" class="cuadrodetexto" value="" size="50" maxlength="50"></td>
                  </tr>
                  <tr valign="baseline">
                    <td nowrap align="right">&nbsp;</td>
                    <td><input name="Submit" type="submit" class="botones" value="Agregar zona"></td>
                  </tr>
                </table>
                <input type="hidden" name="MM_insert" value="form1">
              </form>
              <p>&nbsp;</p>
              on1&#8226;<!-- InstanceEndEditable --></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
<!-- InstanceEnd --></html>
<?php
mysql_free_result($qryzonas);
?>
